package net.xdclass.controller;

import net.xdclass.controller.request.TrafficPageRequest;
import net.xdclass.controller.request.UseTrafficRequest;
import net.xdclass.service.TrafficService;
import net.xdclass.util.JsonData;
import net.xdclass.vo.TrafficVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 功能描述
 *
 * @author ASUS
 * @date 2024/12/10  10:26
 */
@RestController
@RequestMapping("/api/traffic/v1")
public class TrafficController {
    @Autowired
    private TrafficService trafficService;
    @Value("${rpc.token}")
    private String rpcToken;

    /**
     * 使用流量包
     * @param request
     * @param request
     * @return
     */
    @PostMapping("reduce")
    public JsonData useTraffic(@RequestBody UseTrafficRequest request, HttpServletRequest httpServletRequest){
        //获取从dcloud-link发送过来的rpc请求头 TrafficFeignService
        String header = httpServletRequest.getHeader("rpc-token");
        if(header.equals(rpcToken)){
            //具体使用流量包逻辑
            JsonData jsonData = trafficService.reduce(request);
            return jsonData;
        }
        return JsonData.buildError("token错误");
    }
    /**
     * 只能查看可⽤的流ᰁ包，不可⽤的需要去归档数据查
     * @return
     */
    @PostMapping("page")
    public JsonData pageAvailable(
            @RequestBody TrafficPageRequest request
    ) {
        Map<String, Object> pageResult = trafficService.pageAvailable(request);
        return JsonData.buildSuccess(pageResult);
    }
    /**
     * 流ᰁ包详情
     *
     * @return
     */
    @GetMapping("/detail/{trafficId}")
    public JsonData
    detail(@PathVariable("trafficId") Long trafficId
    ) {
        TrafficVO trafficVO = trafficService.detail(trafficId);
        return JsonData.buildSuccess(trafficVO);
    }
}
